package com.song.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.song.api.BaseService;
import com.song.article.mapper.CommentsMapper;
import com.song.article.mapper.CommentsMapperCustom;
import com.song.article.service.ArticlePortalService;
import com.song.article.service.CommentPortalService;
import com.song.pojo.Comments;
import com.song.pojo.vo.ArticleDetailVO;
import com.song.pojo.vo.CommentsVO;
import com.song.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentPortalServiceImpl extends BaseService implements CommentPortalService {

    private static final Logger LOG = LoggerFactory.getLogger(CommentPortalServiceImpl.class);

    @Autowired
    private ArticlePortalService articlePortalService;

    @Autowired
    private Sid sid;

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private CommentsMapperCustom commentsMapperCustom;

    /**
     * 创建评论
     *
     * @param articleId     文章id
     * @param fatherId      父亲id
     * @param commentUserId 评论用户id
     * @param content       内容
     * @param nickname      昵称
     * @return {@code Comments}
     */
    @Transactional
    @Override
    public void createComment(String articleId,
                              String fatherId,
                              String commentUserId,
                              String content,
                              String nickname,
                              String commentUserFace) {

        ArticleDetailVO article = articlePortalService.queryArticleDetail(articleId);

        String commentId = sid.nextShort();

        Comments comments = new Comments();
        comments.setId(commentId);
        comments.setWriterId(article.getPublishUserId());
        comments.setFatherId(fatherId);
        comments.setArticleId(articleId);
        comments.setArticleTitle(article.getTitle());
        comments.setArticleCover(article.getCover());
        comments.setCommentUserId(commentUserId);
        comments.setCommentUserNickname(nickname);
        comments.setCommentUserFace(commentUserFace);
        comments.setContent(content);
        comments.setCreateTime(new Date());
        commentsMapper.insert(comments);

        redis.increment(REDIS_ARTICLE_COMMENT_COUNTS + articleId, 1);
    }

    /**
     * 查询文章评论
     *
     * @param articleId 文章id
     * @param page      页面
     * @param pageSize  页面大小
     * @return {@code List<CommentsVO>}
     */
    @Override
    public PagedGridResult queryArticleComments(String articleId, Integer page, Integer pageSize) {

        Map<String, Object> map = new HashMap<>();
        map.put("articleId", articleId);

        PageHelper.startPage(page, pageSize);
        List<CommentsVO> list = commentsMapperCustom.queryArticleCommentList(map);

        return setPagedGridResult(list, page);
    }

    /**
     * 查询作家的评论管理列表
     *
     * @param writerId 作者id
     * @param page     页面
     * @param pageSize 页面大小
     * @return {@code PagedGridResult}
     */
    @Override
    public PagedGridResult queryWriterCommentsMng(String writerId, Integer page, Integer pageSize) {

        Comments comments = new Comments();
        comments.setWriterId(writerId);

        PageHelper.startPage(page,pageSize);
        List<Comments> list = commentsMapper.select(comments);

        return setPagedGridResult(list,page);
    }

    /**
     * 删除评论
     *
     * @param writerId  作者id
     * @param commentId 评论id
     */
    @Override
    public void deleteComment(String writerId, String commentId) {

        Comments comments = new Comments();
        comments.setWriterId(writerId);
        comments.setId(commentId);

        commentsMapper.delete(comments);
    }
}